package cm367;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.richfaces.model.CalendarDataModel;
import org.richfaces.model.CalendarDataModelItem;

@ManagedBean
public class CalendarModel implements CalendarDataModel {
	private static final String BUSY_DAY_CLASS = "bdc";
	private static final String BOUNDARY_DAY_CLASS = "rf-ca-boundary-dates";

	@ManagedProperty(value = "#{venueService}")
	private VenueService venueservice;
	
	
	private String venuecode;
	private Byte venueperiod;
	

	public String getVenuecode() {
		return venuecode;
	}

	public void setVenuecode(String venuecode) {
		this.venuecode = venuecode;
	}

	public Byte getVenueperiod() {
		return venueperiod;
	}

	public void setVenueperiod(Byte venueperiod) {
		this.venueperiod = venueperiod;
	}

	private boolean checkBusyDay(Calendar calendar) {
		boolean flag = false;
		Booking b = null;
		try {
			b = new Booking();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<BookingDetail> booking = b.getBookinglist();
		Date tmp_date = null;
		for (int i = 0; i < booking.size(); i++) {
			tmp_date = booking.get(i).getBookDate().getTime();			
			if (tmp_date.getYear() == (calendar.getTime()).getYear()
					&& tmp_date.getMonth() == (calendar.getTime()).getMonth()
					&& tmp_date.getDate() == (calendar.getTime()).getDate()) {
				/*
				System.out.println("Part: " + booking.get(i).getBookDatePart());
				System.out.println("Date: " + tmp_date.toString());
				System.out.println("Room: " + booking.get(i).getBookVenus());
				
				System.out.println(venueservice.getPeriod());
				*/
				flag = true;
			}
		}
		return flag;
	}

	public VenueService getVenueservice() {
		return venueservice;
	}

	public void setVenueservice(VenueService venueservice) {
		this.venueservice = venueservice;
	}

	public CalendarDataModelItem[] getData(Date[] dateArray) {
		CalendarDataModelItem[] modelItems = new CalendarModelItem[dateArray.length];
		Calendar current = GregorianCalendar.getInstance();
		Calendar today = GregorianCalendar.getInstance();
		today.setTime(new Date());
		for (int i = 0; i < dateArray.length; i++) {
			current.setTime(dateArray[i]);
			CalendarModelItem modelItem = new CalendarModelItem();
			if (current.before(today)) {
				modelItem.setEnabled(false);
				modelItem.setStyleClass(BOUNDARY_DAY_CLASS);
			} else if (checkBusyDay(current)) {
				modelItem.setEnabled(true);
				modelItem.setStyleClass(BUSY_DAY_CLASS);
			} else {
				modelItem.setEnabled(true);
				modelItem.setStyleClass("");
			}
			modelItems[i] = modelItem;
		}

		return modelItems;
	}

	public Object getToolTip(Date date) {
		// TODO Auto-generated method stub
		return null;
	}
}